/**
 * Copyright (c) 2016-2019 九点科技 All rights reserved.
 *
 * http://www.9dyun.cn
 *
 * 版权所有，侵权必究！
 */
package com.dian.modules.base.service.impl;

import cn.hutool.core.util.StrUtil;
import com.dian.common.enums.ValidEnum;
import com.dian.common.validator.ValidatorUtils;
import com.dian.common.validator.group.AddGroup;
import com.dian.common.validator.group.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dian.common.utils.PageUtils;
import com.dian.common.utils.Query;
import com.dian.common.server.CommonService;
import com.dian.modules.base.dao.WarehouseDao;
import com.dian.modules.base.entity.WarehouseEntity;
import com.dian.modules.base.service.WarehouseService;

/**
 * 仓库信息服务实现类
 *
 * @author Jinag Run
 * @email 1685269193qq.com
 * @date 2019-05-23 09:30:44
 */
@Service("baseWarehouseService")
public class WarehouseServiceImpl extends ServiceImpl<WarehouseDao, WarehouseEntity> implements WarehouseService {
    @Autowired
    public CommonService commonService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<WarehouseEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByAsc("id");
        IPage<WarehouseEntity> page = this.page(new Query<WarehouseEntity>().getPage(params),getQueryWrapper(params) );

        return new PageUtils(page);
    }
    @Override
    public boolean saveInfo(WarehouseEntity baseWarehouseEntity) {

        //数据完整性校验
        this.checkParams(baseWarehouseEntity,AddGroup.class);

        //设置编码等其它属性
        //baseWarehouseEntity.setOrderNo(commonService.getOrderCode("XSD"));


        //保存
        this.save(baseWarehouseEntity);
        return true;
    }


    @Override
    public boolean updateInfo(WarehouseEntity baseWarehouseEntity) {

        //数据完整性校验
        this.checkParams(baseWarehouseEntity, UpdateGroup.class);

        //更新
        this.updateById(baseWarehouseEntity);

        return true;
    }

    @Override
    public boolean deleteInfo(Long[] idList) {

        //删除状态校验
        this.deleteCheckState(idList);

        this.removeByIds(Arrays.asList(idList));

        return true;
    }


    /**
     * 按仓库编码查询
     * @param warehouseCode
     * @return
     */
    @Override
    public WarehouseEntity queryByCode(String warehouseCode) {
        QueryWrapper<WarehouseEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("warehouse_code",warehouseCode);
        queryWrapper.eq("is_valid", ValidEnum.YES.getValue());
        WarehouseEntity goodsEntity =this.getOne(queryWrapper);
        return goodsEntity;
    }



    /***********************************************************************************************/
    /****************************************** 私有方法 ********************************************/
    /***********************************************************************************************/
    /**
     * 删除状态校验
     *
     * @param
     */
    private void deleteCheckState(Long[] idList) {
        for (int index=1;index<=idList.length;index++) {
            WarehouseEntity baseWarehouseEntity =this.getById(idList[index-1]);
            /*
            if(!baseWarehouseEntity.getOrderState().equals(OrderHead_OrderStateEnum.WAITCHECK)){
                throw new RRException(String.format("第%s行中为已审核的订单,不允许删除",index));
            }*/
        }


    }

    /**
     * 参数校验
     * @param
     * @param cls
     */
    private void checkParams(WarehouseEntity baseWarehouseEntity, Class<?> cls) {

        //实体类参数校验
        ValidatorUtils.validateEntity(baseWarehouseEntity, cls);

        //其它业务参数校验

    }
    private QueryWrapper<WarehouseEntity> getQueryWrapper(Map<String, Object> params) {
        QueryWrapper<WarehouseEntity> queryWrapper=new QueryWrapper<>();
        if(!StrUtil.isBlankIfStr(params.get("warehouseCode"))){
            queryWrapper.likeLeft("warehouse_code",params.get("warehouseCode"));
        }
        if(!StrUtil.isBlankIfStr(params.get("warehouseName"))){
            queryWrapper.likeLeft("warehouse_name",params.get("warehouseName"));
        }

        queryWrapper.orderByAsc("id");
        return queryWrapper;
    }

}
